package com.greyu.ysj.utils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.greyu.ysj.config.Constants;
import com.greyu.ysj.config.ResultStatus;
import com.greyu.ysj.model.ResultModel;

import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class CoinQuotations {
	public static ResultModel getCoinInfo() {
		String path = Constants.COIN_SERVER_PATH + "getQuotations";;
		CoinInfo coinQuotations = new CoinInfo();
		// 2 创建okhttpclient对象
		OkHttpClient.Builder client = new OkHttpClient.Builder();
		client.readTimeout(50000, TimeUnit.MILLISECONDS);
		client.writeTimeout(50000, TimeUnit.MILLISECONDS);
		client.connectTimeout(50000, TimeUnit.MILLISECONDS);

		RequestBody body = new FormBody.Builder().build();
		// 3 创建请求方式
		Request request = new Request.Builder().url(path).post(body).build();

		// 4 执行请求操作
		try {
			Response response = client.build().newCall(request).execute();
			if (response.isSuccessful()) {
				String string = response.body().string();
				System.out.println(string+"%%%%%%%");
				JSONObject jsonObject = JSON.parseObject(string);
				if(jsonObject==null) {
					return ResultModel.error(ResultStatus.SERVER_INTERNAL_ERROR);
				}else {
					coinQuotations.setConinSymbol(jsonObject.getString("coinSymbol"));
					coinQuotations.setCoinPrice(jsonObject.getString("coinPrice"));
					coinQuotations.setFluctuation(jsonObject.getString("fluctuation"));
					System.out.println(jsonObject);
					return ResultModel.ok(jsonObject);
				}
			}
		} catch (IOException e) {
			//e.printStackTrace();
			return ResultModel.error(ResultStatus.SERVER_INTERNAL_ERROR);
		}
		return null;
	}
}
